import { any } from "../../core/logic";
import {ComponentType } from "../Component";
import { Light } from "./Light";
import {color} from "../../core/color";
/**
 * 聚光灯
 * @class
 * @extends w.light.Light
 * @memberof w.light
 */
class SpotLight extends Light{
    /**
     * @constructor
     * @param {Number} opts.distance 【可选,缺省:0】衰减距离
     * @param {Number} opts.innerRad 【可选,缺省:0】聚光灯内角
     * @param {Number} opts.outerRad 【可选,Math.PI / 3】聚光灯外角
     * @param {Array} opts.at 【可选,缺省:[0,0,0]】聚光灯目标点
     * @example
     * let light=new w.light.SpotLight({distance:100,intensity:1.0,color:"#ffff00",innerRad:Math.PI/6,outerRad:Math.PI/3,at:[0,0,0]});
     */
    constructor(opts={}){
        opts.type=any(opts.type,ComponentType.SpotLight);
        super(opts);
        this.lightType=2;
        this._distance=any(opts.distance,0);
        this._innerRad = any(opts.innerRad, 0);
        this._outerRad = any(opts.outerRad, Math.PI / 3);
        this._at = opts.at;
        this.setState(true)
        this.autoUpdate=true;
        this.update();
    }
   /**
   * @property {w.component.Trans} 平行光的trans属性。
   * @readonly
   */
    get trans(){
        if(!this.at && this._trans) {
            this.at = [this._trans.position.x, this._trans.position.y, 0];
        }
        return this._trans;
    }
    /**
     * @property {Number} 光源的衰减距离。
     */
    set distance(v){
        this._distance=v;
        this.setState(false,"lightBuffer");
        this.update();
        return this;
    }
    get distance(){
        return this._distance;
    }
   /**
   * @property {Array} 聚光灯的at属性。
   */
    set at(v){
        this._at=v;
        this.setState(false,"lightBuffer");
        this.update();
        return this;
    }
    get at(){
        return this._at;
    }
   /**
   * @property {Number} 聚光灯内角。
   */
    set innerRad(v){
        this._innerRad=v;
        this.setState(false,"lightBuffer");
        this.update();
        return this;
    }
    get innerRad(){
        return this._innerRad;
    }
   /**
   * @property {Number} 聚光灯外角。
   */
    set outerRad(v){
        this._outerRad=v;
        this.setState(false,"lightBuffer");
        this.update();
        return this;
    }
    get outerRad(){
        return this._outerRad;
    }
    getLightBuffer(){
        if(!this.getState("lightBuffer")){
            let _color=color.format(this.color,false);
            this.lightbuffer= [..._color,//0-2
                this.lightType,//3 类型
                ...this.trans.position,//4-6 位置或方向
                this.intensity,//7 强度
                ...this.at,//8-10 距离
                this.distance, //11 innerRad
                Math.cos(this.innerRad),//12 innerRad|width
                Math.cos(this.outerRad),//13 outerRad|height
                0,0//空白
            ];
            this.setState(true,"lightBuffer");
        }
        return this.lightbuffer;
    }
}
export {SpotLight}